package com.open.trace.infrastructure.config;

import com.open.trace.infrastructure.common.util.OverrideThreadPoolExecutor;
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhang kaichao
 * @since 2020/12/29 22:27:16
 */
@Data
@Configuration
public class ThreadConfig {

	private int corePoolSize = 10;

	private int maximumPoolSize = 200;

	private long keepAliveTime = 60;

	private int queue = 10000;

	@Bean
	@Primary
	public ExecutorService executorService() {
		return new OverrideThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS,
				new LinkedBlockingQueue<>(queue), Executors.defaultThreadFactory(),
				new ThreadPoolExecutor.CallerRunsPolicy());
	}

}
